import java.util.ArrayList;
import java.util.List;

public class Solution257 {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public List<String> binaryTreePaths(TreeNode root) {
        List<String> ans=new ArrayList<>();
        List<Integer> list=new ArrayList<>();
        dfs(root,list,ans);
        return ans;
    }

    private void dfs(TreeNode root, List<Integer> list, List<String> ans) {
        if (root==null){
            return;
        }
        if (root.left==null && root.right==null){
            list.add(root.val);
            ans.add(deal(list));
            list.remove(list.size()-1);
            return;
        }
        list.add(root.val);
        dfs(root.left,list,ans);
        dfs(root.right,list,ans);
        list.remove(list.size()-1);
    }

    private String deal(List<Integer> list) {
        if (list.size()==0){
            return "";
        }
        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append(list.get(0));
        for (int i=1;i<list.size();i++){
            stringBuilder.append("->").append(list.get(i));
        }
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        System.out.println(new Solution257().binaryTreePaths(new TreeNode(1,new TreeNode(2,null,new TreeNode(5)),new TreeNode(3))));
    }
}
